<template>
  <div class="login_container">
    <!-- 登录组件 -->
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets//logo.png" alt="" />
      </div>
      <!-- 表单 -->
      <el-form
        label-width="0px"
        class="login_form"
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
      >
        <!-- 用户 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // #3. //表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: "请输入登录名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "登录名长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "密码长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //#3. 添加表单重置方法
    resetLoginForm() {
      //this=>当前组件对象，其中的属性$refs包含了设置的表单ref
      //   console.log(this)
      this.$refs.loginFormRef.resetFields();
      console.log(1111);
    },
    login() {
      // 点击登录按钮发生验证产生回调
      // 发送ajaxpost请求
      // 响应以后拿到服务器tkoenzhi 保存在本地
      // 用编程路由跳转到后台页面
      // 在跳转的时候加一个全局路由判断是是否有token值
      // 有就发行
      // 没有就强制跳转到login
      this.$refs.loginFormRef.validate(async (value) => {
        if (!value) return;
        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status !== 200)
          return this.$message.error("错了哦，这是一条错误消息");
        this.$message({
          message: "恭喜你，这是一条成功消息",
          type: "success",
        });
        console.log(res);
        // 把token存到浏览器上
        window.sessionStorage.setItem("token", res.data.token);
        // 编程路由
        this.$router.push("Home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  position: absolute;
  left: 50%;
  padding: 10px !important;
  transform: translate(-50%, -50%);
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  background: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: #eee;
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>